<template>
  <uni-nav-bar title="老板账户" left-icon="left" @clickLeft="navBack" :fixed="true" statusBar></uni-nav-bar>
  <view class="available">
    <view class="availables">
      <text>姓名</text>
      <text>{{ friendInfor.driver_name }}</text>
    </view>
    <view class="availables">
      <text>手机号</text>
      <text>{{ friendInfor.driver_mobile }}</text>
    </view>
	<view class="availables">
	  <text>总资产</text>
	  <text>{{friendInfor.money}}元</text>
	</view>
    <!-- <view class="availablea" v-if="friendInfor.cart_code_list && friendInfor.cart_code_list.length > 0">
      <text style="font-size: 30rpx">授权车辆</text>
      <view>
        <text v-for="item in friendInfor.cart_code_list">{{ item.cart_code }}</text>
      </view>
    </view> -->
  </view>
  <!-- 选择默认 -->
  <view class="available_mo" v-if="accountID !== 0">
    <text>默认</text>
    <switch :checked="suspen" @change="switch1Change" color="#007AFF" />
  </view>
</template>

<script setup lang="ts">
import { explainempower } from '@/gql/carAuthorized';
import { navBack } from '@/utils/navigator';
import { showLoading, showToast, successToast } from '@/utils/prompt';
import { toPublish } from '@mqtt';
import type { Ref } from 'vue';
import { ref } from 'vue';
import { onLoad } from '@dcloudio/uni-app';

const friendInfor: Ref<any> = ref({});
let accountID = ref(-1);
const suspen = ref(false);
const repeat = ref(true); //放重复点击

onLoad((options) => {
  accountID.value = Number.parseInt(options.id);
  init();
});

function init() {
  showLoading('加载中');
  const payload = {
    query: explainempower,
    variables: {
      id: accountID.value,
      type: 'from_driver_id',
    },
  };
  toPublish('ql/driver/getEmpowerInfo', payload, (obj: any) => {
    const { data } = obj;
    friendInfor.value = data.empower;
    if (data.empower.is_moren === '1') {
      suspen.value = true;
    }
  });
}
function switch1Change() {
  const checked=suspen.value=!suspen.value
  const payload = {
    id: accountID.value,
    is_moren: checked ? '1' : '0',
  };
  showLoading('正在提交');
  if(!repeat.value){
    return
  }
  repeat.value=false
  toPublish('driver/editEmpower', payload, (obj: any) => {
    if (obj.code === 1) {
      repeat.value=true
      successToast(obj.msg);
      init();
    } else {
      repeat.value=true
      showToast(obj.msg);
    }
  });
}
</script>

<style scoped lang="less">
@import url('../authorized/less/accountsDetail.less');
</style>
